package com.bawei.module_home.ui.repair

import android.content.Intent
import android.os.Build
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.activity.viewModels
import androidx.annotation.RequiresApi
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_common.view.FlowLayoutWithAddView
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityRepairBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.RepairModel
import dagger.hilt.android.AndroidEntryPoint
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter


@AndroidEntryPoint
@Route(path = Router.REPAIR_PAGE)
class RepairActivity : BaseActivity<BaseViewModel, ActivityRepairBinding>(){
    override val mLayoutResId: Int
        get() = R.layout.activity_repair

    private val repairViewModel:HomeServiceViewModel  by viewModels()

    private val addresses = arrayOf("北京市朝阳区", "上海市浦东新区", "广州市天河区", "深圳市南山区")

    override fun initView() {
        super.initView()
        mBinding.repairTopbar.title.setText("报修登记")
        mBinding.repairTopbar.more.visibility= View.GONE
        mBinding.repairTopbar.moreText.visibility= View.VISIBLE
        mBinding.repairTopbar.back.setOnClickListener {
            finish()
        }

        // 使用 ArrayAdapter 设置数据


        // 使用 ArrayAdapter 设置数据
        val adapter: ArrayAdapter<String?> = ArrayAdapter<String?>(
            this,
            android.R.layout.simple_spinner_item,
            addresses
        )
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mBinding.repairSelectAddress.setAdapter(adapter)


    }




    override fun initData() {


    }


    @RequiresApi(Build.VERSION_CODES.O)
    override fun initListener() {
        mBinding.repairTopbar.moreText.setOnClickListener {

            // 获取当前日期时间
            val now = LocalDateTime.now()


            // 定义时间格式
            val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")


            // 格式化当前时间
            val currentTime = now.format(formatter)


            repairViewModel.postRepair(RepairModel(
                contactMethod = mBinding.repairPhone.text.toString(),
                faultDescription = mBinding.repairDesc.text.toString(),
                faultPhotos = "123",
                id = 1,
                orderNumber = "111",
                processingStatus = "待处理",
                repairLocation = mBinding.repairAddressTv.text.toString(),
                repairName = AppMMKV.username,
                repairTime = currentTime.toString(),
                userId = AppMMKV.userId
            ))

        }




        mBinding.repairSelectAddress.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                val selectedAddress = (view as TextView).text.toString()
                mBinding.repairAddressTv.setText(selectedAddress)
                mBinding.repairAddressTv.setVisibility(View.GONE)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
                // 未选择时的处理逻辑
            }
        })
        repairViewModel.repairState.observeWithLifecycle(this){
            when(it){
                Empty -> {

                }
                is Failed -> {
                    Log.d("FCC",  "initListener: ${it.message}")
                    ToastUtils.showLong(this,"提交失败")
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"提交成功")
                    finish()
                }
            }
        }

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                FlowLayoutWithAddView.REQUEST_CAMERA -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
                FlowLayoutWithAddView.REQUEST_GALLERY -> {
                    data?.data?.let { uri -> mBinding.flowImage.addImage(uri) }
                }
            }
        }
    }

}